﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Communergy.Common;
using Communergy.Common.Events;
using System.Threading;
using Communergy.Repository;

namespace CommunergyUploader.Exporters
{
    public class CommunergyServerExportCommand : IDataExportCommand
    {
        
        IRepositoryFactory cacheRepository;
        IRepositoryFactory remoteRepository = new WCFRepositoryFactory();
        Meter meter;

        public IRepositoryFactory RemoteRepository
        {
            get { return remoteRepository; }
            set { remoteRepository = value; }
        }
        DateTime lastUploadDateTime = DateTime.Now;


        public CommunergyServerExportCommand(Meter meter)
        {
            this.meter = meter;
          
        }

        #region IDataExporter Members

        public event DataExportHandler DataExportEvent;

        private void RaiseNewDataExportEvent(DataExporterEventArgs de)
        {
            if (DataExportEvent != null)
            {
                DataExportEvent(this, de);
            }
        }

        public void ExportData()
        {
            ExportNewRealtimeRecords();
        }

        private void ExportNewRealtimeRecords()
        {
            IUnitOfWork remoteUnitOfWork = remoteRepository.CreateUnitOfWork();
            IRealtimeRepository remoteRealtimeRepository = remoteRepository.CreateRealtimeRepository(remoteUnitOfWork);

            string localMeterId = meter.MeterId.ToString();
            string remoteMeterId = meter.MeterGuid;

            //TODO - why isn't GetLastElectricityRealtime working properly
            //var lastRealTime = remoteRealtimeRepository.GetLastElectricityRealtime(remoteMeterId);
            //lastUploadDateTime = lastRealTime != null ? lastRealTime.ReadingDateTime : lastUploadDateTime;

            IUnitOfWork localUnitOfWork = cacheRepository.CreateUnitOfWork();
            var localRealtimeRepository = cacheRepository.CreateRealtimeRepository(localUnitOfWork);
            var realtimeEnergyReadingsForUpload = localRealtimeRepository.Get_ElectricityRealtime(localMeterId, lastUploadDateTime, DateTime.Now);

            if (realtimeEnergyReadingsForUpload != null && realtimeEnergyReadingsForUpload.Count > 0)
            {
                try
                {
                    UpdateStatus(string.Format("Sending {0} live records to Communergy", realtimeEnergyReadingsForUpload.Count));
                    remoteRealtimeRepository.Insert_ElectricityRealtime(remoteMeterId, realtimeEnergyReadingsForUpload);
                    UpdateStatus(string.Format("Sent {0} live records to Communergy", realtimeEnergyReadingsForUpload.Count));
                    lastUploadDateTime = DateTime.Now;
                }
                catch (Exception excep)
                {
                    UpdateStatus(excep.Message);
                }
            }
        }

        private void UpdateStatus(string message)
        {
            DataExporterEventArgs de = new DataExporterEventArgs();
            de.Message = message;
            RaiseNewDataExportEvent(de);
        }




        public TimeSpan ExportInterval { get; set; }

        #endregion

        #region IDataExportCommand Members


        public IRepositoryFactory LocalCacheRepository
        {
            get
            {
                return cacheRepository;
            }
            set
            {
                cacheRepository = value;
            }
        }

        #endregion
    }
}
